package com.lijiajian.Service;

import com.lijiajian.base.BaseService;
import com.lijiajian.dao.ModuleMapper;
import com.lijiajian.dao.PermissionMapper;
import com.lijiajian.dao.RoleMapper;
import com.lijiajian.pojo.Permission;
import com.lijiajian.pojo.Role;
import com.lijiajian.query.RoleQuery;
import com.lijiajian.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoleService extends BaseService<Role,Integer> {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private ModuleMapper moduleMapper;

    /**
     * 查询所有角色名
     * @return
     */
    public List<Map<String,Object>> getAllRoles(Integer id){
        return roleMapper.selectAllRoles(id);
    }

    /**
     * 添加角色
     * @param role
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addRole(Role role){
        //参数校验
        AssertUtil.isTrue(StringUtils.isBlank(role.getRoleName()),"用户角色名不能为空");
        //角色名唯一
        Role temp = roleMapper.selectByName(role.getRoleName());
        AssertUtil.isTrue(temp!=null,"角色名已存在");
        //设置默认值
        role.setCreateDate(new Date());
        role.setIsValid(1);
        role.setUpdateDate(new Date());
        //执行添加操作
        AssertUtil.isTrue(roleMapper.insertSelective(role)!=1,"添加失败");

    }

    /**
     * 修改角色
     * @param role
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateRole(Role role){
        //判断角色id非空且数据存在
        AssertUtil.isTrue(role.getId()==null,"待更新记录不存在");
        Role temp = roleMapper.selectByPrimaryKey(role.getId());
        AssertUtil.isTrue(temp==null,"待更新记录不存在");

        //参数校验
        AssertUtil.isTrue(StringUtils.isBlank(role.getRoleName()),"用户角色名不能为空");
        //角色名唯一,非空,判断角色记录存不存在，存在并且角色id与当前更新的角色id不一致，表示不可使用
        temp = roleMapper.selectByName(role.getRoleName());
        AssertUtil.isTrue(temp!=null&&!temp.getId().equals(role.getId()),"角色名已存在");
        //设置默认值
        role.setUpdateDate(new Date());
        //执行修改操作
        AssertUtil.isTrue(roleMapper.updateByPrimaryKeySelective(role)!=1,"添加失败");

    }

    /**
     * 批量删除角色
     * @param roleIds
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteRoles(Integer[] roleIds){
        //判断角色id非空且数据存在
        AssertUtil.isTrue(roleIds==null||roleIds.length==0,"待更新记录不存在");

        //执行删除操作
        AssertUtil.isTrue(roleMapper.updateBatchByIds(roleIds)!=roleIds.length,"删除失败");

    }

    /**
     * 添加资源授权
     * @param roleId
     * @param mIds
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addGrant(Integer roleId,Integer[] mIds){
        //根据角色id查询对应的权限记录
       Integer count= permissionMapper.countPermissionByRoleId(roleId);
       //如果权限记录存在，删除对应的角色所拥有的权限记录
        if(count>0){
            AssertUtil.isTrue(permissionMapper.deletePermissionByRoleId(roleId)!=count,"删除权限记录失败");;
        }
        //如果有权限记录，则添加权限记录
        if(mIds!=null&&mIds.length>0){
            List<Permission> permissionsList=new ArrayList<>();
            //遍历资源id数组
            for(Integer mid:mIds){
                Permission permission = new Permission();
                permission.setModuleId(mid);
                permission.setRoleId(roleId);
                permission.setAclValue(moduleMapper.selectByPrimaryKey(mid).getOptValue());
                permission.setCreateDate(new Date());
                permission.setUpdateDate(new Date());
                permissionsList.add(permission);
            }
            //执行批量添加，判断受影响的行数
            AssertUtil.isTrue(permissionMapper.insertBatch(permissionsList)!=permissionsList.size(),"添加失败");
        }
    }






}
